#include <stdio.h>
#include <stdlib.h>

/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */
#define max 50
int inArea(int r, int c, int imageRowSize, int imageColSize)
{
    if (r >= 0 && r < imageRowSize && c >= 0 && c < imageColSize)
        return 1;
    return 0;
}
void fill(int **image, int imageRowSize, int imageColSize, int old, int newc, int r, int c)
{
    if (!inArea(r, c, imageRowSize, imageColSize))
        return;
    if (image[r][c] != old)
        return;
    if (image[r][c] == -1)
        return;
    image[r][c] = -1;
    fill(image, imageRowSize, imageColSize, old, newc, r + 1, c);
    fill(image, imageRowSize, imageColSize, old, newc, r - 1, c);
    fill(image, imageRowSize, imageColSize, old, newc, r, c + 1);
    fill(image, imageRowSize, imageColSize, old, newc, r, c - 1);
    image[r][c] = newc;
}
int **floodFill(int **image, int imageRowSize, int imageColSize, int sr, int sc, int newColor)
{
    int old = image[sr][sc];
    fill(image, imageRowSize, imageColSize, old, newColor, sr, sc);
    for (int i = 0; i < imageRowSize; i++)
    {
        for (int j = 0; j < imageColSize; j++)
        {
            printf("%3d", image[i][j]);
        }
        printf("\n");
    }
    return image;
}

int main()
{
    /*******************code*******************/
    int a[][3] = {{1, 1, 1}, {1, 1, 0}, {1, 0, 1}};
    int **p = (int **)malloc(sizeof(int *) * 3);
    for (int i = 0; i < 3; i++)
    {
        p[i] = a[i];
    }
    floodFill(p, 3, 3, 1, 1, 2);
    /******************************************/
    fflush(stdin);

    printf("\n\n****************************\n");
    printf("Press Enter key to continue\n");
    getchar();
    return 0;
    /******************************************/
}
